//校园论坛主界面js
// 用户菜单功能
const userAvatar = document.getElementById('userAvatar');
const userDropdown = document.getElementById('userDropdown');

userAvatar.addEventListener('click', () => {
    userDropdown.classList.toggle('active');
});

// 点击页面其他地方关闭菜单
document.addEventListener('click', (e) => {
    if (!userAvatar.contains(e.target)) {
        userDropdown.classList.remove('active');
    }
});

// 板块切换功能
const boardItems = document.querySelectorAll('.board-item');
const currentBoard = document.getElementById('currentBoard');

boardItems.forEach(item => {
    item.addEventListener('click', () => {
        // 移除所有active类
        boardItems.forEach(i => i.classList.remove('active'));
        // 给当前项添加active类
        item.classList.add('active');

        // 更新当前板块标题
        const boardName = item.querySelector('.board-name').textContent;
        currentBoard.textContent = boardName;

        // 更新板块图标
        const iconClass = item.querySelector('i').className;
        document.querySelector('.board-title i').className = iconClass;
    });
});


document.addEventListener('DOMContentLoaded', function() {
    // 从Session获取用户信息（通过JSP表达式预加载）
    const currentUser = {
        nickname: '${user.user_nickname}'.trim() || '未登录',
        avatar: '${user.user_photo}'|| '',
        email: '${user.user_email}',
        bio: '${user.user_bio}',
        major: '${user.user_major}'
    };

    // 检查用户是否已登录
    if (currentUser.nickname === '未登录' ) {
        // 未登录，重定向到登录页面
        window.location.href = '${pageContext.request.contextPath}/login';
        return;
    }

    // 更新用户头像和昵称
    // updateUserInfo(currentUser);

    // 个人信息弹窗逻辑
    const editProfileBtn = document.getElementById('editProfileBtn');
    const profileModal = document.getElementById('profileModal');
    const closeModal = document.getElementById('closeModal');
    const cancelEdit = document.getElementById('cancelEdit');
    const saveProfile = document.getElementById('saveProfile');

    editProfileBtn.addEventListener('click', function(e) {
         e.preventDefault();
        profileModal.style.display = 'block';
        // 填充表单数据（已通过JSP预填充）
    });

    function updateUserInfo(user) {
        const userAvatar = document.getElementById('userAvatar');
        const userNameLink = document.querySelector('#userDropdown a:first-child');

        // 设置头像文本为昵称首字
        userAvatar.querySelector('span').textContent = user.nickname.substring(0, 1);

        // 如果有用户头像图片，更新头像样式
        // if (user.avatar && user.avatar !== '' && user.avatar !== 'null') {
        //     userAvatar.style.backgroundImage = `url('${user.avatar}')`;
        //     userAvatar.style.backgroundSize = 'cover';
        //     userAvatar.querySelector('span').textContent = ''; // 有图片时隐藏文字
        // }

        // 更新个人主页链接显示的昵称
        if (userNameLink) {
            userNameLink.innerHTML = `<i class="fas fa-user"></i> ${user.nickname}`;
        }
    }

    // 关闭弹窗
    closeModal.addEventListener('click', function() {
        profileModal.style.display = 'none';
    });

    cancelEdit.addEventListener('click', function() {
        profileModal.style.display = 'none';
    });

    // 保存个人信息（前端示例，实际需调用后端接口）
    saveProfile.addEventListener('click', function() {
        const newNickname = document.getElementById('username').value;
        const newEmail = document.getElementById('email').value;
        const newBio = document.getElementById('bio').value;
        const newMajor = document.getElementById('major').value;

        // 这里应调用后端API更新用户信息
        alert('个人信息已更新！');
        profileModal.style.display = 'none';

        // 更新页面显示的用户信息
        currentUser.nickname = newNickname;
        updateUserInfo(currentUser);
    });

    // 点击页面其他地方关闭弹窗
    window.addEventListener('click', function(e) {
        if (e.target === profileModal) {
            profileModal.style.display = 'none';
        }
    });

});

// 修改个人信息弹窗
const editProfileBtn = document.getElementById('editProfileBtn');
const profileModal = document.getElementById('profileModal');
const closeModal = document.getElementById('closeModal');

editProfileBtn.addEventListener('click', (e) => {
    e.preventDefault();
    profileModal.classList.add('active');
});

closeModal.addEventListener('click', () => {
    profileModal.classList.remove('active');
});

// 点击弹窗外部关闭
profileModal.addEventListener('click', (e) => {
    if (e.target === profileModal) {
        profileModal.classList.remove('active');
    }
});

// 互动按钮功能
const actionBtns = document.querySelectorAll('.action-btn');

actionBtns.forEach(btn => {
    btn.addEventListener('click', function() {
        const action = this.querySelector('span').textContent;
        const icon = this.querySelector('i');

        // 添加点击效果
        this.style.transform = 'scale(0.95)';
        setTimeout(() => {
            this.style.transform = '';
        }, 200);

        // 根据操作类型执行不同逻辑
        if (action === '点赞' || action.includes('点赞')) {
            const countSpan = this.querySelector('span');
            const currentCount = parseInt(countSpan.textContent.match(/\d+/)[0]) || 0;
            countSpan.textContent = `点赞(${currentCount + 1})`;

            // 切换图标样式
            if (icon.classList.contains('far')) {
                icon.classList.remove('far');
                icon.classList.add('fas');
                this.style.color = '#3498db';
            }
        }

        if (action === '收藏') {
            if (icon.classList.contains('far')) {
                icon.classList.remove('far');
                icon.classList.add('fas');
                this.style.color = '#f39c12';
                this.querySelector('span').textContent = '已收藏';
            } else {
                icon.classList.remove('fas');
                icon.classList.add('far');
                this.style.color = '';
                this.querySelector('span').textContent = '收藏';
            }
        }

        if (action === '举报') {
            alert('举报已提交，管理员将尽快处理！');
        }
    });
});

// 点击发布新帖可以成功跳转发布新帖界面的事件监听
document.querySelector('.new-post-btn').addEventListener('click', function() {
    window.location.href = 'forum_post_create.jsp';
});